package com.budwk.app.sqfw.controllers.admin;

import cn.dev33.satoken.annotation.SaCheckLogin;
import cn.hutool.core.collection.CollectionUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.json.JSONUtil;
import com.budwk.app.sqfw.dto.SqfwCompleteflowDTO;
import com.budwk.app.sqfw.dto.SqfwWordContentDTO;
import com.budwk.app.sqfw.models.Sqfw_reportFile;
import com.budwk.app.sqfw.services.SqfwReportFileService;
import com.budwk.app.sqfw.vo.SqfwReportFileVO;
import com.budwk.app.sqfwV2.dto.RallyPdfSaveDTO;
import com.budwk.starter.common.openapi.annotation.ApiDefinition;
import com.budwk.starter.common.openapi.annotation.ApiFormParams;
import com.budwk.starter.common.openapi.annotation.ApiOperation;
import com.budwk.starter.common.openapi.annotation.ApiResponses;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.common.result.Result;
import com.budwk.starter.log.annotation.SLog;
import com.budwk.starter.security.utils.SecurityUtil;
import lombok.extern.slf4j.Slf4j;
import org.json.JSONObject;
import org.nutz.dao.Cnd;
import org.nutz.ioc.loader.annotation.Inject;
import org.nutz.ioc.loader.annotation.IocBean;
import org.nutz.lang.Strings;
import org.nutz.mvc.annotation.At;
import org.nutz.mvc.annotation.Ok;
import org.nutz.mvc.annotation.POST;
import org.nutz.mvc.annotation.Param;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * @author ZY.Wang
 * @since 2023/7/4
 */
@IocBean
@At("/platform/sqfw/reportFile")
@ApiDefinition(tag = "报告文件")
@Slf4j
public class SqfwReportFileController {

    @Inject
    private SqfwReportFileService sqfwReportFileService;

    @At("/listReportFile")
    @Ok("json")
    @POST
    @ApiOperation(name = "查询报告文件")
    @SaCheckLogin
    @SLog(tag = "查询报告文件", value = "${args[0]}")
    public Result<?> listReportFile(@Param("..") Sqfw_reportFile bean, @Param("pageOrderName") String pageOrderName, @Param("pageOrderBy") String pageOrderBy) {
        List<SqfwReportFileVO> list = sqfwReportFileService.listReportFile(bean, pageOrderName, pageOrderBy);
        return Result.success(list);
    }


    @At("/insertRallyBriefing")
    @Ok("json")
    @POST
    @ApiOperation(name = "插入文档数据")
    @SLog(tag = "活动总结", value = "${args[0]}")
    @ApiFormParams()
    @ApiResponses
    @SaCheckLogin
    public Result insertRallyBriefing(@Param("..") SqfwCompleteflowDTO sqfwReportFile) {
        sqfwReportFileService.insertRallyBriefing(sqfwReportFile);
        return Result.success();
    }


    @At("/getReportBySqfwId")
    @Ok("json")
    @POST
    @ApiOperation(name = "插入文档数据")
    @SLog(tag = "活动总结查询简报", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getReportBySqfwId(@Param(value = "sqfwId") String sqfwId) {
           return Result.success(sqfwReportFileService.getReportBySqfwId(sqfwId));
    }


    @At
    @Ok("json")
    @POST
    @ApiOperation(name = "插入文档数据")
    @SLog(tag = "活动总结查询简报", value = "${args[0]}")
    @SaCheckLogin
    public Result<?> getReportByRallyId(@Param(value = "rallyId") String rallyId) {
        return Result.success(sqfwReportFileService.getReportByRallyId(rallyId));
    }

    @At
    @Ok("json")
    @POST
    @SaCheckLogin
    public Result<?> getReportPage( @Param("pageNo") int pageNo,@Param("pageSize") int pageSize,@Param("parpmName") String parpmName) {
        List<String> type=new ArrayList<>();
        String userId = SecurityUtil.getUserId();
        type.add("活动总结");
        Cnd cnd= Cnd.NEW();;
        cnd.and(Sqfw_reportFile::getServiceRecordType,"in",type).and(Sqfw_reportFile::getCreateBy,"=",userId).desc(Sqfw_reportFile::getCreateDate);
        if(StrUtil.isNotBlank(parpmName)){
            cnd.and(Sqfw_reportFile::getReportName,"like","%" + parpmName + "%");
        }
        Pagination  pagination = sqfwReportFileService.listPage(pageNo,pageSize,cnd);
        if(CollectionUtil.isNotEmpty(pagination.getList())){
            pagination.getList(Sqfw_reportFile.class).forEach(s->{
                s.setCompleteflowDTO(JSONUtil.toBean(s.getDocDetail(), SqfwCompleteflowDTO.class));
            });
        }
        return Result.data(pagination);
    }
}
